<?php

class SchoolsController extends AppController {

	var $name = 'Schools';
	var $uses = array('User', 'School', 'Application');
	
	function beforeFilter() {
		parent::beforeFilter();
		if (isset($this->passedArgs[0]) && $this->Auth->user('group_id') == 2) {
			$model = ($this->params['action'] == 'delete')? 'School': 'Application';
  		$aco = $this->Acl->Aco->findByModelAndForeignKey($model, $this->passedArgs[0]);
    	$aro = $this->Acl->Aro->findByModelAndForeignKey('User', $this->Auth->user('id'));
    	if (!$this->Acl->check($aro['Aro'], $aco['Aco'])) {
      	$this->Session->setFlash($this->Auth->authError, 'flash_failure');
        $this->redirect($this->referer());
    	}
		}
	}
	
	/**
	* Default function for application.
	*/
	function index ($app_id=null) {
		
		if (!$app_id) {
			$this->Session->setFlash(__('No valid application id: Please contact gradsec@psych.uoregon.edu', true));
			$this->redirect($this->referer());
		}
		
		$data = $this->School->findAllByApplicationId($app_id);
		$this->set('app_id', $app_id); 		
		$this->set(compact('data'));
		$this->set('info', $this->_getContentMsg('schools_index_info'));
		
	} //end index
	
	
	/**
	*add a course to this courselist
	*/
	function add ($app_id=null) {
		
		if (!empty($this->data)) {
			$this->School->create();		
			if($this->School->save($this->data)) {
				$app_id = $this->School->field('application_id');
				$stat = 0;
				if ($this->School->find('count', array(
																'conditions' => array(
																'School.application_id' => $app_id))) > 0) {
					$stat= 1;
				}
				$this->Application->change_status($app_id, 'edhistory', $stat, '');
				
				
				$this->Session->setFlash("Successfully Updated School List", 'flash_success', null, 'sl');
			} else {
				$this->Session->setFlash(__('Error encountered, Unable to save school.', true), 'flash_failure', null, 'sl');
			}
		} else {
			$this->Session->setFlash(__('Error encountered, No Data.', true), 'flash_failure', null, 'sl');
		}
		
		$data = $this->School->findAllByApplicationId($app_id);
		$this->set('app_id', $app_id); 		
		$this->set(compact('data'));
		
		if ($this->RequestHandler->isAjax()) {
					$this->render('school_list', 'ajax');
				} else {
					$this->redirect(array('action'=>'index', $app_id));	
				}
	}
	
	/**
	*Delete a course.
	*/
	function delete ($id=null) {
		
		if (!$id) {
			$this->Session->setFlash(__('Invalid id for school', true), 'flash_failure');
			$this->redirect($this->referer());
		}
		
		$this->School->id = $id;
		$app_id = $this->School->field('application_id');
		
		if($this->School->delete($id)) {
			$stat = 0;
			if ($this->School->find('count', array(
																'conditions' => array(
																'School.application_id' => $app_id))) > 0) {
				$stat= 1;
			}	
			$this->Application->change_status($app_id, 'edhistory', $stat, '');
		} else {
			$this->Session->setFlash(__('Error deleting school.', true), 'flash_failure');
		}
		
		$data = $this->School->findAllByApplicationId($app_id);
		$this->set('app_id', $app_id); 		
		$this->set(compact('data'));
		
		if ($this->RequestHandler->isAjax()) {
			$this->render('school_list', 'ajax');
		} else {
			$this->redirect(array('action'=>'index', $app_id));	
		}
		
	}

}

